#! /usr/bin/env python

import mathex

mid=[1,3,7,9]
ht = [[3,3],[3,7],[7,3],[7,7]]
hd = [2,3,5,7,]
tl = [3,7,]

def match(n):
    l=len(n)
    for i in range(l):
        if not mathex.isPrime(asmb(n[i:])) or not mathex.isPrime(asmb(n[:-i])):
            return False
    return True

def asmb(n):
    if not n:
        return 2
    return reduce(lambda x,y:x*10+y,n,0)

def genmid(bit):
    if bit==0:
        yield []
    else:
        for a in mid:
            for b in genmid(bit-1):
                yield [a] + b

def main():
    all=[]
    bit = 0
    while len(all)<11:
        for p in genmid(bit):
            for k in hd:
                for j in tl:
                    n=[k]+p+[j]
                    if match(n):
                        all.append(n)
        bit += 1

    print len(all)
    print all
    print sum([asmb(n) for n in all])

if __name__ == '__main__':
    main()
    
